package com.zhiwang.xiaoqu.ui

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.zhiwang.xiaoqu.R
import com.zhiwang.xiaoqu.di.annotation.Injectable
import com.zhiwang.xiaoqu.di.component.DaggerUserComponent
import com.zhiwang.xiaoqu.di.module.AutoDisposeModule
import com.zhiwang.xiaoqu.ktextension.setImmersive
import com.zhiwang.xiaoqu.mvp.model.MoneyDetailModel
import com.zhiwang.xiaoqu.mvp.presenter.UserPresenter
import com.zhiwang.xiaoqu.ui.about.ServiceActivity
import kotlinx.android.synthetic.main.activity_money_detail_list.*
import kotlinx.android.synthetic.main.item_money_detail.view.*
import kotlinx.android.synthetic.main.item_money_detail_date.view.*
import quickStartActivity
import javax.inject.Inject
import kotlin.collections.ArrayList

class MoneyDetailListActivity : BaseActivity(), Injectable {

    val dataList = arrayListOf<MoneyDetailModel>()

    val adapter = MoneyDetailAdapter()

    var page = 1

    @Inject
    lateinit var userPresenter: UserPresenter

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setImmersive(Color.TRANSPARENT, true)
        setContentView(R.layout.activity_money_detail_list)
        setSupportActionBar(toolbar)
        recyclerView.adapter = adapter
        recyclerView.addItemDecoration(SpecialItemDecoration())
        userPresenter.getMoneyDetailList(page, ::onLoadSuccess, ::onError)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.service_menu_dark, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == R.id.service) {
            quickStartActivity(ServiceActivity::class.java)
            return true
        }
        return super.onOptionsItemSelected(item)
    }


    fun onLoadSuccess(list: ArrayList<MoneyDetailModel>) {


        val oldList = ArrayList(dataList)

        dataList.addAll(list)

        DiffUtil.calculateDiff(BaseListActivity.DiffCallBackImpl(oldList, dataList)).dispatchUpdatesTo(adapter)

    }

    inner class SpecialItemDecoration : RecyclerView.ItemDecoration {

        //每个item圆角半径
        val radius = 24

        // 日期 顶部距离
        val dateHeaderSpace = 35

        //游戏item 与日期之间的灰色距离
        val itemHeaderSpace = 5

        //相连的 item 之间的距离
        val itemSpace = 2

        val paint = Paint(Paint.ANTI_ALIAS_FLAG)

        constructor() {
            paint.color = Color.WHITE
            paint.style = Paint.Style.FILL
        }


        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
            val position = parent.getChildAdapterPosition(view)

            val type = dataList[position].type()

            if (type == 0)
                outRect.set(0, dateHeaderSpace + radius, 0, radius)
            else {
                val prevType = dataList[position - 1].type()

                val nextType = if (position + 1 < parent.adapter!!.itemCount) dataList[position + 1].type() else -1

                if (prevType == 0) {
                    outRect.set(0, itemHeaderSpace + radius, 0, 0)
                } else if (prevType == 1) {
                    outRect.set(0, itemSpace, 0, 0)
                    if (nextType == 0)
                        outRect.set(0, itemSpace, 0, radius)
                } else if (nextType == 0) {
                    outRect.set(0, itemSpace, 0, radius)
                }
            }

        }

        override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {

            for (i in 0 until parent.childCount) {

                val child = parent.getChildAt(i)
                val position = parent.getChildAdapterPosition(child)

                val type = dataList[position].type()

                if (type == 0) {
                    val left = child.left.toFloat()
                    val top = child.top - radius.toFloat()
                    val right = child.right.toFloat()
                    val bottom = child.top + radius.toFloat()

                    c.drawRoundRect(
                        left, top, right,
                        bottom, radius.toFloat(), radius.toFloat(), paint
                    )

                    c.drawRoundRect(
                        left,
                        child.bottom.toFloat() - radius,
                        right,
                        child.bottom + radius.toFloat(),
                        radius.toFloat(),
                        radius.toFloat(),
                        paint
                    )
                } else {
                    val prevType = dataList[position - 1].type()

                    val nextType = if (position + 1 < parent.adapter!!.itemCount) dataList[position + 1].type() else -1

                    if (prevType == 0) {
                        val left = child.left
                        val top = child.top - radius
                        val right = child.right
                        val bottom = child.top + radius

                        c.drawRoundRect(
                            left.toFloat(), top.toFloat(), right.toFloat(),
                            bottom.toFloat(), radius.toFloat(), radius.toFloat(), paint
                        )

                    } else if (prevType == 1) {
                        c.drawRect(
                            child.left.toFloat(),
                            (child.top - itemSpace).toFloat(), (child.left + 90).toFloat(), child.top.toFloat(), paint
                        )

                        c.drawRect(
                            child.right - 90f,
                            child.top - itemSpace.toFloat(),
                            child.right.toFloat(),
                            child.top.toFloat(),
                            paint
                        )

                        if (nextType == 0)

                        //底部圆角
                            c.drawRoundRect(
                                child.left.toFloat(),
                                child.bottom - radius.toFloat(),
                                child.right.toFloat(),
                                child.bottom.toFloat() + radius,
                                radius.toFloat(),
                                radius.toFloat(),
                                paint
                            )

                    } else if (nextType == 0) {
                        /*outRect.set(0, itemSpace, 0, radius)*/
                        //两条白线
                        c.drawRect(
                            child.left.toFloat(),
                            (child.top - itemSpace).toFloat(), (child.left + 90).toFloat(), child.top.toFloat(), paint
                        )

                        c.drawRect(
                            child.right - 90f,
                            child.top - itemSpace.toFloat(),
                            child.right.toFloat(),
                            child.top.toFloat(),
                            paint
                        )

                    }
                }
            }
        }
    }

    inner class MoneyDetailAdapter : RecyclerView.Adapter<MoneyItemViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MoneyItemViewHolder {

            if (viewType == 0)
                return MoneyItemViewHolder(layoutInflater.inflate(R.layout.item_money_detail_date, parent, false))

            return MoneyItemViewHolder(layoutInflater.inflate(R.layout.item_money_detail, parent, false))
        }

        override fun getItemCount() = dataList.size

        override fun getItemViewType(position: Int): Int {
            return dataList[position].type()
        }

        override fun onBindViewHolder(holder: MoneyItemViewHolder, position: Int) {
            holder.bind()
        }

    }


    inner class MoneyItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bind() {
            val data = dataList[adapterPosition]

            if (data.type() == 0) {
                itemView.date.text = data.date()
                itemView.sum.text = data.rewardSum()
            } else {
                itemView.money_desc.text = data.name()
                itemView.money_time.text = data.date()
                itemView.money_sum.text = data.rewardSum()
            }
        }

    }

}
